#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import lxml.html

import sys


def loop_for_parse(t):
    result = []
    support_scheme = ["http",
                      "socks4",
                      "socks5"]
    try:
        assert t in support_scheme
    except:
        raise NotImplementedError
    if t == "http":
        urls = ["http://www.cz88.net/proxy/index.shtml",
                "http://www.cz88.net/proxy/" + t + "_2.shtml",
                "http://www.cz88.net/proxy/" + t + "_3.shtml",
                "http://www.cz88.net/proxy/" + t + "_4.shtml",
                "http://www.cz88.net/proxy/" + t + "_5.shtml",
                "http://www.cz88.net/proxy/" + t + "_6.shtml",
                "http://www.cz88.net/proxy/" + t + "_7.shtml",
                "http://www.cz88.net/proxy/" + t + "_8.shtml",
                "http://www.cz88.net/proxy/" + t + "_9.shtml",
                "http://www.cz88.net/proxy/" + t + "_10.shtml"]
    elif t == "socks4":
        urls = ["http://www.cz88.net/proxy/socks4.shtml",
                "http://www.cz88.net/proxy/" + t + "_2.shtml",
                "http://www.cz88.net/proxy/" + t + "_3.shtml"]
    elif t == "socks5":
        urls = ["http://www.cz88.net/proxy/socks5.shtml",
                "http://www.cz88.net/proxy/" + t + "_2.shtml"]
    else:
        pass
    for url in urls:
        try:
            print "[*] Parsing %s now" % url
            proxy = parse_proxy_list(url)
        except:
            break
        result += proxy
    return result


def parse_proxy_list(url):
    r = requests.get(url)
    assert r.ok
    #----------------------------------------
    proxy_list = []
    root = lxml.html.fromstring(r.content)
    ips = root.xpath('//tr/td[1]/text()')[1::]
    ports = root.xpath('//tr/td[2]/text()')[1::]
    for ip, port in zip(ips, ports):
        # print ip, port
        proxy_list.append(ip + ':' + port)
    return proxy_list

if __name__ == '__main__':
    t = sys.argv[1]
    proxy_list = loop_for_parse(t)
    for i in range(len(proxy_list)):
        # print proxy_list[i]
        proxy_list[i] += '\n'
    with open('proxy_list/' + t + '_proxy_cz.txt', 'wb') as f:
        f.writelines(proxy_list)
